class ShowsController < ApplicationController
  before_filter :require_admin,           :only => [:new,   :create,  :edit,    :update,  :destroy]
  before_filter :find_show,               :only => [:show,  :edit,    :update,  :destroy]
  before_filter :find_list_items,         :only => [:show,  :new,     :update,  :create,  :edit]  

  def index
    if params[:display] == "previous"
      @shows = Show.find(:all, :conditions => ["start_time < ?", Time.now], :order => 'start_time desc').paginate :page => params[:page], :per_page => 30
    elsif params[:display] == "upcoming"
      @shows = Show.find(:all, :conditions => ["start_time > ?", Time.now], :order => 'start_time asc').paginate :page => params[:page], :per_page => 30
    else
      @shows = Show.find(:all, :order => 'start_time desc').paginate :page => params[:page], :per_page => 30
    end
  end

  def show
    @comment = Comment.new
    respond_to do |show|
      show.html  {}
      show.xml   {render :xml => @show.xml}
    end
  end

  def new
    @show = Show.new
  end

  def create
    @show = Show.new(params[:show])
    @show.save!
    flash[:update] = 'The show has been added'
    redirect_to show_url(@show)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = "Oops, that didn't work, see below."
    render :action => 'new'
  end

  def edit
  end

  def update
    @show.update_attributes!(params[:show])
    flash[:update] = 'The show information has been updated.'
    redirect_to show_url(@show)
  rescue ActiveRecord::RecordInvalid
    flash[:error] = "Oops, that didn't work, see below."
    render :action => 'edit'
  end

  def destroy
    @show.destroy
    flash[:update] = 'The show has been deleted.'
    redirect_to shows_url
  end

protected
  def require_login
    unless logged_in?
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def require_admin
    if logged_in?
      unless current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_to :back
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_show
    @show = Show.find params[:id]
  end
  def find_list_items
    @songs    = Song.find     :all, :order => 'created_at desc'
    @albums   = Album.find    :all, :order => 'created_at desc'
    @artwork  = Artwork.find  :all, :order => 'created_at desc', :conditions => ["thumbnail IS ?", nil]
    @photos   = Photo.find    :all, :order => 'created_at desc', :conditions => ["thumbnail IS ?", nil]
    @videos   = Video.find    :all, :order => 'created_at desc'
    @users    = User.find     :all, :order => 'created_at desc'
  end
end